<?php
require_once("../data/common.php");
if ($user->logged_in == FALSE) {
	die("Login first.");
}
//var_dump($user->logged_in_user_id);
echo '<script type="text/javascript" src="media/js/mytracks.js"></script>';


        $sql = "SELECT
                        tr.device_id AS device_id,
                        tr.first_id AS first_id,
                        tr.last_id AS last_id,
                        tr.track_id AS track_id,
                        u.username AS username,
                        tr.created AS created,
                        tr.start_place AS start_place,
                        tr.end_place AS end_place,
			tr.privacy AS privacy,
                        gd.time_received AS time_received
                FROM
                        users u,
                        tracks tr,
                        users_devices ud,
                        gps_data gd
                WHERE   
                        (tr.last_id = gd.id AND
                                tr.device_id=ud.id AND
                                ud.user_id=u.id) AND
                        u.id = ?
                ORDER BY tr.created DESC;";

        $stmt = $dbh->prepare($sql);
        $stmt->execute(array($user->logged_in_user_id));
        $rows = $stmt->fetchAll();
	print '<table border="0">';
	print "<tr><td>Username</td><td>Track time</td><td>Starting place</td><td>Finishing place</td><td>Public</td></tr>"; //The "public" should be a link to a short faq answer.
	foreach ($rows as $rs) {
		$start_place = $rs["start_place"];
		$end_place = $rs["end_place"];
		$now = time();
		$updated = strtotime($rs["time_received"]);
		if($start_place == null) $start_place = updateLocation("start_place",$rs["track_id"],$rs["first_id"]);
		if($end_place == null) {
			if (($now-$updated) > (60*10) ) {
				$end_place = updateLocation("end_place",$rs["track_id"],$rs["last_id"]);
			} else {
				$end_place = "TRACK NOT FINISHED";
			}
		}
		if ($rs["privacy"] == 0) {
			$privacy = "checked=\"checked\"";
		} else {
			$privacy = "";
		}
		printf('<script type="text/javascript">var track_%s = \'%b\';</script>', $rs["track_id"], $rs["privacy"]);
		printf('<tr><td><a href="#" onclick="self.parent.viewtrack(\'%s\',%d,%d,%d,%d,\'%s\')">%s</a></td><td>%s</td><td>%s</td><td>%s</td><td><a href="?page=share&id=%s&height=400&width=640" class="thickbox" >Share this track!</a></td><td>
<input type="checkbox" %s onclick="change_privacy(\'%s\',track_%s); if (track_%s == 1) {track_%s = 0} else {track_%s = 1};"></input>
</td></tr>',
			$site_url,
			$rs["device_id"],
			$rs["first_id"],
			$rs["last_id"],
			$rs["track_id"], 
			$rs["username"], 
			$rs["username"],
			$rs["created"], 
			$start_place, 
			$end_place,
			$rs["track_id"],
			$privacy,
			$rs["track_id"],
			$rs["track_id"],
			$rs["track_id"],
			$rs["track_id"],
			$rs["track_id"]
			);
	}
	print '</table>';

?>
